package com.eddie.hhcook;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eddie.hhcook.annotation.ContentView;
import com.eddie.hhcook.annotation.ViewInject;
import com.eddie.hhcook.annotation.ViewUtils;
import com.eddie.hhcook.base.BaseActivity;
import com.eddie.hhcook.entity.User;
import com.eddie.hhcook.qqapi.QQAccessTokenKeeper;
import com.eddie.hhcook.utils.Util;
import com.eddie.hhcook.view.CircleImageView;
import com.eddie.hhcook.view.pulltozoomview.PullToZoomScrollViewEx;
import com.tencent.connect.UserInfo;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

@ContentView(R.layout.activity_pull_to_zoom_scroll_view)
public class PersonCenterActivity extends BaseActivity{

    private static final String TAG = PersonCenterActivity.class.getSimpleName();


    private CircleImageView user_image;
    private TextView user_nickname;
    private RelativeLayout user_logout;
    @ViewInject(R.id.scroll_view)
    private PullToZoomScrollViewEx scrollView;
    private Handler mHandler;


    @Override
    public void initView() {
        ViewUtils.inject(this);
        loadViewForCode();
        DisplayMetrics localDisplayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
        int mScreenHeight = localDisplayMetrics.heightPixels;
        int mScreenWidth = localDisplayMetrics.widthPixels;
        LinearLayout.LayoutParams localObject = new LinearLayout.LayoutParams(mScreenWidth, (int) (9.0F * (mScreenWidth / 16.0F)));
        scrollView.setHeaderLayoutParams(localObject);
       user_image = (CircleImageView) scrollView.getPullRootView().findViewById(R.id.iv_user_head);
      user_nickname = (TextView) scrollView.getPullRootView().findViewById(R.id.tv_user_name);
      user_logout = (RelativeLayout) scrollView.getPullRootView().findViewById(R.id.user_logout);
        getUserInfo();
        mHandler = getmHandler();
        user_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tencent tencent = HHcookApp.getInstance().getmTencent();
                if (tencent != null){
                    tencent.logout(PersonCenterActivity.this);
                    QQAccessTokenKeeper.clear(PersonCenterActivity.this);
                    Intent intent = new Intent(PersonCenterActivity.this,MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void setListener() {

    }

    private void loadViewForCode() {
        View headView = LayoutInflater.from(this).inflate(R.layout.profile_head_view, null, false);
        View zoomView = LayoutInflater.from(this).inflate(R.layout.profile_zoom_view, null, false);
        View contentView = LayoutInflater.from(this).inflate(R.layout.profile_content_view, null, false);
        scrollView.setHeaderView(headView);
        scrollView.setZoomView(zoomView);
        scrollView.setScrollContentView(contentView);
    }



    private Handler getmHandler(){
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0) {
                    JSONObject response = (JSONObject) msg.obj;
                    if (response.has("nickname")) {
                        try {
                            user_nickname.setText(response.getString("nickname"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }else if(msg.what == 1){
                    Bitmap bitmap = (Bitmap)msg.obj;
                    user_image.setImageBitmap(bitmap);
                }
            }
        };
        return mHandler;
    }


    private void getUserInfo() {
        UserInfo userInfo = new UserInfo(this,HHcookApp.getInstance().getmTencent().getQQToken());
        userInfo.getUserInfo(new IUiListener() {
            @Override
            public void onComplete(final Object response) {
                final User user  = new User();
                Message msg = new Message();
                msg.obj = response;
                msg.what = 0;
                mHandler.sendMessage(msg);
                new Thread(){
                    @Override
                    public void run() {
                        JSONObject json = (JSONObject)response;
                        if(json.has("figureurl")){
                            Bitmap bitmap = null;
                            try {
                                bitmap = Util.getbitmap(json.getString("figureurl_qq_2"));
                                user.setBitmap(bitmap);
                                HHcookApp.getInstance().setUser(user);
                            } catch (JSONException e) {
                                    e.printStackTrace();
                            }
                            Message msg = new Message();
                            msg.obj = bitmap;
                            msg.what = 1;
                          mHandler.sendMessage(msg);
                        }
                    }

                }.start();
            }

            @Override
            public void onError(UiError uiError) {

            }

            @Override
            public void onCancel() {

            }
        });
    }

}
